//preamble 
clear
set more off
// Define local paths for different output destinations
local results "Output/Results"
cap mkdir "`results'"
local appendix "Output/DataAppendixOutput"
cap mkdir "`appendix'"

// Import the processed Excel dataset from the AnalysisData folder
import excel using "Data/AnalysisData/processed_data.xlsx", firstrow


//re-establish panel data format
drop company_id
encode RIC, gen(company_id)
xtset company_id DATE
///////////////////////////////////////////////////////////////////////////////
graph set window fontface "Times New Roman"

//Regression 1a: squeeze period borrow fees for high SI stocks - squeeze period as regressor 
xtset company_id DATE
preserve
keep if HIGH_SI == 1
xtreg log_borrow_fee SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe 
outreg2 using "`results'/regression1_results.doc", replace
predict residuals, residuals
hist residuals, normal title("Model 1: Histogram of Residuals", size(large)) ///
    xlabel(, labsize(large)) ylabel(, labsize(large)) ///
    xtitle("Residuals", size(large)) ytitle("Density", size(large))
graph export "`appendix'/reg1ahist.png", replace width(1200)
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Model 1: Residuals vs. Fitted Values", size(large)) ///
    xlabel(, labsize(large) grid) ylabel(, labsize(large) grid) ///
    xtitle("Fitted Values", size(large)) ytitle("Residuals", size(large)) ///
    yline(0, lcolor(red))
graph export "`appendix'/reg1aplot.png", replace width(1200)
restore
 

//Regression 1b: borrow fees for high SI stocks - loan supply as regressor 
preserve
keep if HIGH_SI == 1
xtreg log_borrow_fee log_loan_supply VIX_CLOSE VOLUME, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore
//Regression 2a: borrow fees for high SI stocks - post period as regressor **best results**
preserve 
keep if HIGH_SI == 1
xtreg log_borrow_fee POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN log_loan_supply, fe
outreg2 using "`results'/regression2_results.doc", replace
predict residuals, residuals
hist residuals, normal title("Model 2: Histogram of Residuals", size(large)) ///
    xlabel(, labsize(large)) ylabel(, labsize(large)) ///
    xtitle("Residuals", size(large)) ytitle("Density", size(large))
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Model 2: Residuals vs. Fitted Values", size(large)) ///
    xlabel(, labsize(large) grid) ylabel(, labsize(large) grid) ///
    xtitle("Fitted Values", size(large)) ytitle("Residuals", size(large)) ///
    yline(0, lcolor(red))
restore

//Regression 2b: post period borrow fees for high SI stocks - loan supply as regressor 
preserve 
keep if HIGH_SI == 1
keep if POST_SQUEEZE1 == 1
xtreg log_borrow_fee log_loan_supply VIX_CLOSE VOLUME, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
graph export "`appendix'/reg2bhist.png", replace width(1200)
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
graph export "`appendix'/reg2bplot.png", replace width(1200)
restore

//////control regressions - 

//Regression 3a: squeeze period borrow fees for control stocks - squeeze period as regressor 
preserve 
keep if HIGH_SI == 0
xtreg log_borrow_fee SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore

//Regression 3b: borrow fees for control stocks - loan supply as regressor 
preserve 
keep if HIGH_SI == 0
xtreg log_borrow_fee log_loan_supply VIX_CLOSE VOLUME, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore

//Regression 4a: fees for control stocks - post period as regressor 
preserve 
keep if HIGH_SI == 0
xtreg log_borrow_fee POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN log_loan_supply, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore

//Regression 4b: post period borrow fees for control stocks - loan supply as regressor 
preserve 
keep if HIGH_SI == 0
keep if POST_SQUEEZE1 == 1
xtreg log_borrow_fee log_loan_supply VIX_CLOSE VOLUME, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore

///////////////////////////////////////////////////////////////////////////////
//supply decreases during squeeze for high SI group
//regression 5a
preserve
keep if HIGH_SI == 1
xtreg log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe
outreg2 using "`results'/regression3_results.doc", replace
predict residuals, residuals
hist residuals, normal title("Model 3: Histogram of Residuals", size(large)) ///
    xlabel(, labsize(large)) ylabel(, labsize(large)) ///
    xtitle("Residuals", size(large)) ytitle("Density", size(large))
graph export "`appendix'/reg5ahist.png", replace width(1200)
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Model 3: Residuals vs. Fitted Values", size(large)) ///
    xlabel(, labsize(large) grid) ylabel(, labsize(large) grid) ///
    xtitle("Fitted Values", size(large)) ytitle("Residuals", size(large)) ///
    yline(0, lcolor(red))
graph export "`appendix'/reg5aplot.png", replace width(1200)
restore

//supply increases after squeeze for high SI group
//regression 5b
preserve
keep if HIGH_SI == 1
xtreg log_loan_supply POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN, fe
outreg2 using "`results'/regression4_results.doc", replace
predict residuals, residuals
hist residuals, normal title("Model 4: Histogram of Residuals", size(large)) ///
    xlabel(, labsize(large)) ylabel(, labsize(large)) ///
    xtitle("Residuals", size(large)) ytitle("Density", size(large))
graph export "`appendix'/reg5bhist.png", replace width(1200)
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Model 4: Residuals vs. Fitted Values", size(large)) ///
    xlabel(, labsize(large) grid) ylabel(, labsize(large) grid) ///
    xtitle("Fitted Values", size(large)) ytitle("Residuals", size(large)) ///
    yline(0, lcolor(red))
graph export "`appendix'/reg5bplot.png", replace width(1200)
restore


//no significant supply change during squeeze for control group
////regression 6a
preserve
keep if HIGH_SI == 0
xtreg log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore
//no significant supply change after squeeze for control group
////regression 6b
preserve
keep if HIGH_SI == 0
xtreg log_loan_supply POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore

///////////////////////////////////////////////////////////////////////////////
///test for non-linear relationship between loan supply/fees
//regression 7
preserve
keep if HIGH_SI == 1
xtreg log_borrow_fee log_loan_supply c.log_loan_supply#c.log_loan_supply VOLUME, fe
restore
//examining whether supply-fee relationship changed during squeeze
//regression 8a  - high SI
preserve
keep if HIGH_SI == 1
xtreg log_borrow_fee log_loan_supply SQUEEZE_PERIOD1 c.log_loan_supply#i.SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore
//regression 8b - low SI
preserve
keep if HIGH_SI == 0
xtreg log_borrow_fee log_loan_supply SQUEEZE_PERIOD1 c.log_loan_supply#i.SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore
//examining lagging effects in how supply responded to fee change
//regression 9
preserve
keep if HIGH_SI == 1
xtreg log_loan_supply L(1/3).log_borrow_fee VIX_CLOSE VOLUME, fe
predict residuals, residuals
hist residuals, normal title("Histogram of Residuals")
predict fitted_values, xb       // Generate fitted values
scatter residuals fitted_values, msize(small) ///
    title("Residuals vs. Fitted Values") ///
    xlabel(, grid) ylabel(, grid) ///
    yline(0, lcolor(red))
restore

/////////////////////////////////////////////////////////////////
//ROBUSTNESS CHECKS - Clustered SE's, Newey-West SE's for 4 models

//NEWEY WEST///
//1 Borrow Fees during squeeze
xtset company_id DATE
preserve
keep if HIGH_SI == 1
newey log_borrow_fee SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, lag(7) force
restore
preserve
keep if HIGH_SI == 0
newey log_borrow_fee SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, lag(7) force
restore
//2 Borrow Fees after squeeze
preserve 
keep if HIGH_SI == 1
newey log_borrow_fee POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN log_loan_supply, lag(7) force
restore
preserve 
keep if HIGH_SI == 0
newey log_borrow_fee POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN log_loan_supply, lag(7) force
restore
//3 Loan Supply during squeeze
preserve
keep if HIGH_SI == 1
newey log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, lag(7) force
restore
preserve
keep if HIGH_SI == 0
newey log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, lag(7) force
restore
//4 Loan Supply after squeeze
preserve
keep if HIGH_SI == 1
newey log_loan_supply POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN, lag(7) force
restore
preserve
keep if HIGH_SI == 0
newey log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, lag(7) force
restore

//Clustered SE's///
//1 Borrow Fees during squeeze
xtset company_id DATE
preserve
keep if HIGH_SI == 1
xtreg log_borrow_fee SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe vce(cluster company_id)
restore
preserve
keep if HIGH_SI == 0
xtreg log_borrow_fee SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe vce(cluster company_id)
restore
//2 Borrow Fees after squeeze
preserve 
keep if HIGH_SI == 1
xtreg log_borrow_fee POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN log_loan_supply, fe vce(cluster company_id)
restore
preserve 
keep if HIGH_SI == 0
xtreg log_borrow_fee POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN log_loan_supply, fe vce(cluster company_id)
restore
//3 Loan Supply during squeeze
preserve
keep if HIGH_SI == 1
xtreg log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe vce(cluster company_id)
restore
preserve
keep if HIGH_SI == 0
xtreg log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe vce(cluster company_id)
restore
//4 Loan Supply after squeeze
preserve
keep if HIGH_SI == 1
xtreg log_loan_supply POST_SQUEEZE1 VIX_CLOSE VOLUME D_RETURN, fe vce(cluster company_id)
restore
preserve
keep if HIGH_SI == 0
xtreg log_loan_supply SQUEEZE_PERIOD1 VIX_CLOSE VOLUME D_RETURN, fe vce(cluster company_id)
restore




